import { Game } from "./game";
import { MenuMode } from "./menu";
import { Vector2 } from "./vector";

export class Control {
  keys_ = new Map<string, boolean>();

  constructor(private game: Game) {}

  init() {
    window.addEventListener("keydown", event => {
      this.keys_.set(event.code, true);

      if (event.key === "Escape") {
        if (this.game.menu.mode === MenuMode.stats) {
          this.game.menu.showCredits();
        } else if (this.game.menu.mode === MenuMode.credits) {
          this.game.startNewGame();
        } else {
          this.game.togglePause();
        }
      }
      // 添加 B 键处理  
      if (event.key === "b" || event.key === "B") {  
        this.game.engine.camera.triggerCinematicEffect();  
      }  
      // 在 keydown 事件处理中添加  
      if (event.key === "p" || event.key === "P") {  
        // 按P键播放介绍剧本  
        this.game.engine.scriptManager.playScript("intro");  
      }  
        
      if (event.key === "o" || event.key === "O") {  
        // 按O键停止当前剧本  
        this.game.engine.scriptManager.stopCurrentScript();  
      }
      // 在 keydown 事件处理中添加  
      if (event.key === "m" || event.key === "M") {  
        // 按E键在玩家附近生成敌人  
        const playerPos = this.game.engine.player.body_.pos;  
        const spawnPos = new Vector2(playerPos.x + 100, playerPos.y - 50);  
        this.game.engine.enemyManager.spawnEnemy("walker", spawnPos);  
      }
    });

    window.addEventListener("keyup", event => {
      this.keys_.set(event.code, false);
    });
  }
}
